package com.liyu.rabbit.consumer;

import com.liyu.rabbit.factory.RabbitFactory;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 李誉
 * @date 2022-06-23 22:21
 * @description 死信消费者
 */
public class DlxConsumer {

    // 死信队列
    public static final String DLX_HELLO_QUEUE = "dlx.hello.queue";
    public static final String DLX_HELLO_EXCHANGE = "dlx.hello.exchange";

    // 正常队列
    public static final String NORMAL_HELLO_QUEUE = "normal.hello.queue";
    public static final String NORMAL_HELLO_EXCHANGE = "normal.hello.exchange";


    public static void main(String[] args) throws Exception {
//        ttlExpire();
//        nack();
        maxlen();
    }

    /**
     * 进入死信队列的方式一
     * 消息过期
     * @throws Exception
     */
    static void ttlExpire() throws Exception {
        // 创建信道
        Channel channel = RabbitFactory.createNewChannel();

        /* start----------------------------------- 普通正常队列 ----------------------------- */
        Map<String,Object> arguments = new HashMap<>();
        // 死信交换机
        arguments.put("x-dead-letter-exchange", DLX_HELLO_EXCHANGE);
        // 死信路由key
        arguments.put( "x-dead-letter-routing-key" , DLX_HELLO_QUEUE );
        // 过期时间:建议生产者设置
//        arguments.put( "x-message-ttl" , 1000 );
        channel.queueDeclare(NORMAL_HELLO_QUEUE, false, false, false, arguments);
        channel.queueBind(NORMAL_HELLO_QUEUE, NORMAL_HELLO_EXCHANGE, NORMAL_HELLO_QUEUE);
        /* end----------------------------------- 普通正常队列 ----------------------------- */

        /* start--------------------------------- 死信队列 ---------------------------------- */
//        这里需要死信交换机
        channel.exchangeDeclare(DLX_HELLO_EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.queueDeclare(DLX_HELLO_QUEUE, false, false, false, null);
        channel.queueBind(DlxConsumer.DLX_HELLO_QUEUE, DLX_HELLO_EXCHANGE, DLX_HELLO_QUEUE);
        DeliverCallback dlxcallback = (consumerTag, message) -> {
            System.out.println("死信队列"+ DlxConsumer.DLX_HELLO_QUEUE+"消费了一条过期消息：" + new String(message.getBody(), StandardCharsets.UTF_8));
        };
        channel.basicConsume(DlxConsumer.DLX_HELLO_QUEUE, true, dlxcallback, consumerTag -> {});
        /* end--------------------------------- 死信队列 ---------------------------------- */
    }

    /**
     * 进入死信队列的方式二
     * 否定确认
     * @throws Exception
     */
    static void nack() throws Exception {
        // 创建信道
        Channel channel = RabbitFactory.createNewChannel();

        /* start----------------------------------- 普通正常队列 ----------------------------- */
        Map<String,Object> arguments = new HashMap<>();
        // 死信交换机
        arguments.put("x-dead-letter-exchange", DLX_HELLO_EXCHANGE);
        // 死信路由key
        arguments.put( "x-dead-letter-routing-key" , DLX_HELLO_QUEUE );
        // 过期时间:建议生产者设置
//        arguments.put( "x-message-ttl" , 1000 );
        channel.queueDeclare(NORMAL_HELLO_QUEUE, false, false, false, arguments);
        channel.queueBind(NORMAL_HELLO_QUEUE, NORMAL_HELLO_EXCHANGE, NORMAL_HELLO_QUEUE);
        DeliverCallback callback = (consumerTag, message) -> {
            System.out.println("普通队列"+NORMAL_HELLO_QUEUE+"拒绝消费了一条消息：" + new String(message.getBody(), StandardCharsets.UTF_8));
            channel.basicNack(message.getEnvelope().getDeliveryTag(), false, false);
        };
        channel.basicConsume(NORMAL_HELLO_QUEUE, false, callback, consumerTag -> {});
        /* end----------------------------------- 普通正常队列 ----------------------------- */

        /* start--------------------------------- 死信队列 ---------------------------------- */
//        这里需要死信交换机
        channel.exchangeDeclare(DLX_HELLO_EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.queueDeclare(DLX_HELLO_QUEUE, false, false, false, null);
        channel.queueBind(DlxConsumer.DLX_HELLO_QUEUE, DLX_HELLO_EXCHANGE, DLX_HELLO_QUEUE);
        DeliverCallback dlxcallback = (consumerTag, message) -> {
            System.out.println("死信队列"+ DlxConsumer.DLX_HELLO_QUEUE+"消费了一条被拒绝的消息：" + new String(message.getBody(), StandardCharsets.UTF_8));
        };
        channel.basicConsume(DlxConsumer.DLX_HELLO_QUEUE, true, dlxcallback, consumerTag -> {});
        /* end--------------------------------- 死信队列 ---------------------------------- */
    }

    /**
     * 进入死信队列的方式三
     * 超出队列在最大长度
     * @throws Exception
     */
    static void maxlen() throws Exception {
        // 创建信道
        Channel channel = RabbitFactory.createNewChannel();

        /* start----------------------------------- 普通正常队列 ----------------------------- */
        Map<String,Object> arguments = new HashMap<>();
        // 死信交换机
        arguments.put("x-dead-letter-exchange", DLX_HELLO_EXCHANGE);
        // 死信路由key
        arguments.put( "x-dead-letter-routing-key" , DLX_HELLO_QUEUE );
        // 设置队列最大长度
        arguments.put("x-max-length", 2);
        // 过期时间:建议生产者设置
//        arguments.put( "x-message-ttl" , 1000 );
        channel.queueDeclare(NORMAL_HELLO_QUEUE, false, false, false, arguments);
        channel.queueBind(NORMAL_HELLO_QUEUE, NORMAL_HELLO_EXCHANGE, NORMAL_HELLO_QUEUE);
//        取消消费逻辑，保证消息存在于队列中不被消费
        DeliverCallback callback = (consumerTag, message) -> {
            System.out.println("普通队列"+NORMAL_HELLO_QUEUE+"拒绝消费了一条消息：" + new String(message.getBody(), StandardCharsets.UTF_8));
            channel.basicNack(message.getEnvelope().getDeliveryTag(), false, false);
        };
        channel.basicConsume(NORMAL_HELLO_QUEUE, false, callback, consumerTag -> {});

        /* end----------------------------------- 普通正常队列 ----------------------------- */

        /* start--------------------------------- 死信队列 ---------------------------------- */
//        这里需要死信交换机
        channel.exchangeDeclare(DLX_HELLO_EXCHANGE, BuiltinExchangeType.DIRECT);
        channel.queueDeclare(DLX_HELLO_QUEUE, false, false, false, null);
        channel.queueBind(DlxConsumer.DLX_HELLO_QUEUE, DLX_HELLO_EXCHANGE, DLX_HELLO_QUEUE);
        DeliverCallback dlxcallback = (consumerTag, message) -> {
            System.out.println("死信队列"+ DlxConsumer.DLX_HELLO_QUEUE+"消费了一条被溢出的消息：" + new String(message.getBody(), StandardCharsets.UTF_8));
        };
        channel.basicConsume(DlxConsumer.DLX_HELLO_QUEUE, true, dlxcallback, consumerTag -> {});
        /* end--------------------------------- 死信队列 ---------------------------------- */
    }
}
